package com.fractal;

import android.util.Log;

public class Palette 
{
	public static final int RANDOM = 1;
	public static final int FIRE = 2;
	public static final int SKY = 3;
	public static final int RAINBOW = 4;
	public static final int RAPID = 5;
	public static final int BLACKWHITE = 6;
	
	private final int solidColor = 0xFF000000;
	private int[] colors;
	private int maxColor;

	public Palette(int type)
	{
		switch (type)
		{
		case FIRE:
		{
			int[] c = { 0x000000, 0xFF0000, 0xFF8C00, 0xFFFF00 };
			int[] a = { 10,10,10,10};
			makeGradient(c,a,0xFFFFFFFF);
			break;
		}
		case SKY:
		{
			int[] c = { 0x191970, 0xAFEEEE, 0xE0FFFF, 0xAFEEEE };
			int[] a = { 10,10,10,10};
			makeGradient(c,a,0xFFFFD700);
			break;
		}
		case RAINBOW:
		{
			int[] c = { 0xFF0000, 0xFF7F00, 0xFFFF00, 0x00FF00, 0x0000FF, 0x8B00FF, 0x0000FF, 0x00FF00, 0xFFFF00, 0xFF7F00, 0xFF0000 };
			int[] a = {10,10,10,10,10,10,10,10,10,10,10};
			makeGradient(c,a,0xFF8B00FF);
			break;
		}
		case RAPID:
		{
			int[] c = { 0xFFFFFFFF, 0xFF0000FF, 0xFFFF00FF, 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FFFF };
			colors = c;
			maxColor = 0xFF000000;
			break;
		}
		case BLACKWHITE:
		{
			int[] c = { 0xFFFFFF, 0x000000 };
			int[] a = { 50, 50 };
			makeGradient(c, a, 0xFF000000);
			break;
		}
		
		}
		
	}
	
	// return the count of actual palettes -- so not including random
	public static int count()
	{
		return 5;
	}
	
	public int getARGB(int n)
	{
		return colors[n%colors.length];
	}

	public int getARGB()
	{
		return maxColor;
	}
	
	private void makeGradient(int[] checkpoints, int[] amounts, int max)
	{
		this.maxColor = max;

		int sum = 0;
		for (int i : amounts)
			sum += i;
		
		this.colors = new int[sum];
		
		int coloridx = 0;
		int from, to, add;
		float dR, dG, dB;
		for (int i = 0; i < amounts.length; i++)
		{
			from = checkpoints[i];
			to = checkpoints[(i+1)%checkpoints.length];
			dR =  (((to & 0x00FF0000) >> 16 ) - ((from & 0x00FF0000 ) >> 16)) / (amounts[i]*1.0f);  
			dG =  (((to & 0x0000FF00) >> 8 ) - ((from & 0x0000FF00 ) >> 8)) / (amounts[i]*1.0f);  
			dB =   ((to & 0x000000FF) - (from & 0x000000FF )) / (amounts[i]*1.0f);

			Log.d("palette","From "+((from & 0x00FF0000) >> 16)+ " " + ((from & 0x0000FF00) >> 8)+ " " + ((from & 0x000000FF)));
			Log.d("palette","To "+((to & 0x00FF0000) >> 16)+ " " + ((to & 0x0000FF00) >> 8)+ " " + ((to & 0x000000FF)));
			Log.d("palette","Deltas " + dR + " " + dG + " " + dB);
			
			for (int c = 0; c < amounts[i]; c++, coloridx++)
			{
				add = (Math.round(dR*c) << 16) + (Math.round(dG*c) << 8) + Math.round(dB*c);  
				this.colors[coloridx] = ((from + add) | solidColor);
			}
		}
	}
}
